import net.zjitc.domain.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Administrator on 2017/3/1.
 */
public class YlgHiberate {
    /**
     * 增加
     */
    @Test
    public void configYlg(){
        //创建configuration实例
        Configuration configuration=new Configuration();
        //自动加载配置文件
        configuration.configure("hibernate.cfg.xml");
        //构建sessionFactory实例(绘画工厂)
        SessionFactory sf=configuration.buildSessionFactory();
        /*session实例*/
        Session session=sf.openSession();
        /*开始事物*/
        Transaction transaction=session.beginTransaction();
        User user=new User();
        user.setUsername("ylg");
        user.setPassword("123456");

        session.save(user);

        transaction.commit();
        session.close();


    }
    /**
     * 查询
     */
    @Test
    public void getYlg(){
        //创建configuration实例
        Configuration configuration=new Configuration();
        configuration.configure("hibernate.cfg.xml");
        SessionFactory sf=configuration.buildSessionFactory();
        Session session=sf.openSession();
        Transaction transaction=session.beginTransaction();
//        User user= (User) session.get(User.class,4);
        User user=null;
              user=(User) session.createQuery(
                      "from User  a where a.username=? and a.password=?")
                      .setParameter(0,"54")
                      .setParameter(1,"45")
                      .uniqueResult();

        System.out.println(user);
        transaction.commit();
        session.close();
    }
    /**
     * 删除
     */
    @Test
    public void delete(){
        Configuration configuration=new Configuration();
        configuration.configure("hibernate.cfg.xml");
        SessionFactory sf=configuration.buildSessionFactory();
        Session session=sf.openSession();
        Transaction transaction=session.beginTransaction();
        User user= (User) session.get(User.class,1);
        session.delete(user);
        transaction.commit();
        session.close();
    }
    /**
     * 更改
     */
    @Test
    public void update(){
        Configuration configuration=new Configuration();
        configuration.configure("hibernate.cfg.xml");
        SessionFactory sf=configuration.buildSessionFactory();
        Session session=sf.openSession();
        Transaction transaction=session.beginTransaction();
        User user= (User) session.load(User.class,2);
        user.setUsername("lzk");
        session.update(user);
        transaction.commit();
        session.close();
    }
    @Test
    public void testBean(){
        ApplicationContext ac =new ClassPathXmlApplicationContext("spring-config.xml");
        SessionFactory sf= (SessionFactory) ac.getBean("sessionFactory");
        System.out.println(sf);
    }
}
